package com.bjsxt.shop.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.users.dao.IAAdminsDao;
import com.bjsxt.shop.users.dao.IARoleDao;
import com.bjsxt.shop.users.pojo.AAdmins;
import com.bjsxt.shop.users.pojo.ARole;
import com.bjsxt.shop.users.service.IUsersService;
/**
 * 用户相关的service实现类
 * @author hou
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	/*-----管理员的实现类开始---*/
	
	/**
	 * 添加一条管理员
	 */
	@Override
	public JSONObject saveAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.adminsDao.save(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//存储data
			JSONObject dataJOSN = new JSONObject();
			dataJOSN.put("id", admins.getId()+"");
			dataJOSN.put("effect", res+"");
			resultJSON.put("data", dataJOSN);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	/**
	 * 更新一条记录
	 */
	@Override
	public JSONObject updateAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.adminsDao.update(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//储存data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	/**
	 * 删除一条记录
	 */
	@Override
	public JSONObject deleteAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.adminsDao.delete(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//存储data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			//失败
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	/**
	 * 查询单条记录
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		
		return this.adminsDao.findOne(condMap);
	}
	/**
	 * 查询多条记录，带分页
	 */
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		//为关键字加上%，实现模糊查询
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil !=null)
		{
			//分页
			//查询总结记录数
			condMap.put("pageCount", "true");
			List<AAdmins>roleList = this.adminsDao.findCondList(condMap);
			//去总记录数
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			//查询数据
			condMap.put("pageCount", "false");
			//limit ?,?
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findCondList(condMap);
		}
		return this.adminsDao.findCondList(condMap);
	}
	/*-----管理员的实现类结束---*/
	
	/*-----角色的实现类开始---*/
	/**
	 * 添加一条记录
	 */
	@Override
	public JSONObject saveRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.roleDao.save(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//储存data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id",role.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			//失败
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	/**
	 * 更新一条记录
	 */
	@Override
	public JSONObject updateRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.roleDao.update(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//储存data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			//失败
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	/**
	 * 删除一条记录
	 */
	@Override
	public JSONObject deleteRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		//影响条数
		int res = this.roleDao.delete(role);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//储存data
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			//失败
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	/**
	 * 查询单条记录
	 */
	@Override
	public ARole findOneRoleSevice(Map<String, Object> condMap)
	{
		
		return this.roleDao.findOne(condMap);
	}
	/**
	 * 查询多条记录加分页
	 */
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		//为关键字加上%，实现模糊查询
		if(condMap.get("keyword") != null )
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil !=null)
		{
			//分页
			//查询总记录数
			condMap.put("pageCount", "true");
			List<ARole>roleList = this.roleDao.findCondList(condMap);
			//获取总记录数
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			//查询数据
			condMap.put("pageCount", "false");
			
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.findCondList(condMap);
		}
		return this.roleDao.findCondList(condMap);
	}
	/*-----角色的实现类结束---*/
}
